package com.lxl.filter;

import javax.servlet.*;
import java.io.IOException;

public class FormatFilter implements Filter {
    public FormatFilter(){}
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
        request.setCharacterEncoding("utf-8");
        String type = request.getParameter("type");
        if ( type != null && "xml".equals(type)){
            response.setContentType("text/xml;charset=utf-8");
        } else {
            response.setContentType("text/json;charset=utf-8");
        }

        filterChain.doFilter(request, response);
    }

    @Override
    public void destroy() {}
}
